//
//  PromptTimeView.m
//  MobileOffice
//
//  Created by jjkj on 16/11/2.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PromptTimeView.h"

@implementation PromptTimeView

- (instancetype)initWithFrame:(CGRect)frame message:(NSString *)message prompt:(NSString *)prompt{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor clearColor];
        
        UIView *backView=[[UIView alloc]initWithFrame:frame];
        backView.backgroundColor=[UIColor blackColor];
        backView.alpha=0.4;
        [self addSubview:backView];
        
        UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(50, 200, IPHONE_WIDTH-2*50, 150)];
        contentView.backgroundColor=[UIColor whiteColor];
        contentView.layer.masksToBounds=YES;
        contentView.layer.cornerRadius=8;
        [self addSubview:contentView];
        
//        _myTitleLabel=[MyControl creatLabelWithFrame:CGRectMake(8, 8, contentView.width-16, 20) text:title];
//        _myTitleLabel.textAlignment=NSTextAlignmentCenter;
//        _myTitleLabel.font=FONT_16;
//        [contentView addSubview:_myTitleLabel];
        
        //内容信息
        _messageLabel=[MyControl creatLabelWithFrame:CGRectMake(8, 8, contentView.width-16, 20) text:message];
        _messageLabel.textColor=[UIColor blackColor];
        _messageLabel.numberOfLines=0;
        _messageLabel.font=FONT_15;
        [contentView addSubview:_messageLabel];
        CGFloat messageHeight=[LZXHelper textHeightFromTextString:message width:_messageLabel.width fontSize:15]+3;
        [_messageLabel changeView_Height:messageHeight];
        
        //勾选框
        _selectButton=[MyControl creatButtonWithFrame:CGRectMake(60, _messageLabel.y+_messageLabel.height+10, 18, 18) target:self sel:@selector(selectButtonClick:) tag:100 image:@"icon_notselect@2x" title:nil];
        [_selectButton setImage:[UIImage imageNamed:@"icon_select@2x"] forState:UIControlStateSelected];
//        _selectButton.layer.borderColor=[[UIColor blackColor]CGColor];
//        _selectButton.layer.borderWidth=2;
//        _selectButton.layer.masksToBounds=YES;
//        _selectButton.layer.cornerRadius=2;
        [contentView addSubview:_selectButton];
        
        //提示语
        _promptLabel=[MyControl creatLabelWithFrame:CGRectMake(_selectButton.x+_selectButton.width+15, _selectButton.y-1, contentView.width-(_selectButton.x+_selectButton.width+10), 20) text:prompt];
        _promptLabel.font=[UIFont boldSystemFontOfSize:15];
        [contentView addSubview:_promptLabel];
        
        NSArray *title=@[@"确认",@"取消"];
        CGFloat buttonWidth=(contentView.width-2*120)/3;
        for (int i=0; i<title.count; i++) {
            _submitButton=[MyControl creatButtonWithFrame:CGRectMake(buttonWidth*(i+1)+120*i,_selectButton.y+_selectButton.height+10, 120, 40) target:self sel:@selector(submitClick:) tag:120+i image:nil title:title[i]];
            _submitButton.layer.masksToBounds=YES;
            _submitButton.layer.cornerRadius=5;
            [_submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            if (i==0) {
                _submitButton.backgroundColor=COLOR_MIDBLUE;
            }else{
                _submitButton.backgroundColor=[UIColor orangeColor];
            }
            [contentView addSubview:_submitButton];
        }
        [contentView changeView_Height:_submitButton.y+_submitButton.height+15];
        [contentView changeView_Y:(IPHONE_HEIGHT-contentView.height)/2-64];
        //        _lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 49, IPHONE_WIDTH, 1)];
        //        _lineLabel.backgroundColor=BACKCOLOR_GRAY;
        //        [self.contentView addSubview:_lineLabel];
    }
    return self;
}

- (void)selectButtonClick:(UIButton *)button{
    button.selected=!button.selected;
}

- (void)submitClick:(UIButton *)button{
    if (button.tag==120) {
        if (_selectButton) {
            if (_selectButton.selected) {
                [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:NO] forKey:@"switch0"];
                [[NSUserDefaults standardUserDefaults]synchronize];
            }
        }
        [self removeFromSuperview];
        if (_promptTimeBlock) {
            _promptTimeBlock();
        }
    }else{
        [self removeFromSuperview];
        if (_promptTimeCancelBlock) {
            _promptTimeCancelBlock();
        }
    }
}

- (void)dealloc{
    NSLog(@"PromptTimeViewDealloc");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
